package edu.tgc.service.code;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import edu.tgc.entity.code.TbCodeDegf;
import edu.tgc.entity.code.TbCodeJbType;
import edu.tgc.entity.code.TbCodeJglx;
import edu.tgc.entity.code.TbCodeJzyt;
import edu.tgc.entity.code.TbCodeQdgf;
import edu.tgc.entity.code.TbCodeReportState;
import edu.tgc.entity.code.TbCodeTaskState;
import edu.tgc.entity.code.TbCodeXmState;
import edu.tgc.entity.code.TbCodeXmType;
import edu.tgc.entity.code.TbScale;
import edu.tgc.repository.code.DegfRepository;
import edu.tgc.repository.code.JbTypeRepository;
import edu.tgc.repository.code.JglxRepository;
import edu.tgc.repository.code.JzytRepository;
import edu.tgc.repository.code.QdgfRepository;
import edu.tgc.repository.code.ReportStateRepository;
import edu.tgc.repository.code.ScaleRespository;
import edu.tgc.repository.code.TaskStateRepository;
import edu.tgc.repository.code.XmStateRepository;
import edu.tgc.repository.code.XmTypeRepository;

@Service
public class CodeService {

    @Autowired
    private DegfRepository degfDAO;
    @Autowired
    private JbTypeRepository jbTypeDAO;
    @Autowired
    private JglxRepository jglxDAO;
    @Autowired
    private JzytRepository jzytDAO;
    @Autowired
    private QdgfRepository qdgfDAO;
    @Autowired
    private ReportStateRepository reportStateDAO;
    @Autowired
    private ScaleRespository scaleDAO;
    @Autowired
    private TaskStateRepository taskStateDAO;
    @Autowired
    private XmStateRepository xmStateDAO;
    @Autowired
    private XmTypeRepository xmTypeDAO;

    public void deleteDegf(Integer id) {
        this.degfDAO.delete(id);

    }

    public void deleteJbTypeById(Integer id) {
        this.jbTypeDAO.delete(id);
    }

    public void deleteJglx(Integer id) {
        this.jglxDAO.delete(id);
    }

    public void deleteJzyt(Integer id) {
        this.jzytDAO.delete(id);

    }

    public void deleteQdgf(Integer id) {
        this.qdgfDAO.delete(id);

    }

    public void deleteReportState(Integer id) {
        this.reportStateDAO.delete(id);

    }

    public void deleteScale(UUID id) {
        this.scaleDAO.delete(id);

    }

    public void deleteTaskstate(Integer id) {
        this.taskStateDAO.delete(id);

    }

    public void deletexmStateById(Integer id) {
        this.xmStateDAO.delete(id);

    }

    public void deleteXmTypeById(Integer id) {
        this.xmTypeDAO.delete(id);

    }

    public List<Map<String, Object>> findAllJbType() {
        List<Map<String, Object>> comb = new ArrayList<>();
        List<TbCodeJbType> list = this.jbTypeDAO.findAll();
        for (TbCodeJbType type : list) {
            Map<String, Object> item = new HashMap<>();
            item.put("id", type.getId());
            item.put("text", type.getName());
            comb.add(item);
        }
        return comb;
    }

    public List<Map<String, Object>> findAllJglxTree() {
        List<TbCodeJglx> list = this.jglxDAO.findAll();
        List<Map<String, Object>> maps = new ArrayList<>();
        for (TbCodeJglx jglx : list) {
            Map<String, Object> m = new HashMap<>();
            m.put("id", jglx.getId());
            m.put("text", jglx.getName());
            maps.add(m);
        }
        return maps;
    }

    public List<Map<String, Object>> findAllXmType() {
        List<Map<String, Object>> tree = new ArrayList<>();
        List<TbCodeXmType> list = this.xmTypeDAO.findAll();
        for (TbCodeXmType type : list) {
            Map<String, Object> node = new HashMap<>();
            node.put("id", type.getId());
            node.put("text", type.getName());
            tree.add(node);
        }
        return tree;
    }

    public TbCodeDegf findDegfPage(Integer id) {

        return this.degfDAO.findOne(id);
    }

    public Page<TbCodeDegf> findDegfPage(Pageable pageable) {

        return this.degfDAO.findAll(pageable);
    }

    public List<Map<String, Object>> findDegfTreebox() {
        List<TbCodeDegf> list = this.degfDAO.findAll();
        List<Map<String, Object>> maps = new ArrayList<>();
        for (TbCodeDegf degf : list) {
            Map<String, Object> m = new HashMap<>();
            m.put("id", degf.getId());
            m.put("text", degf.getName());
            maps.add(m);
        }
        return maps;
    }

    public TbCodeJbType findJbTypeById(Integer id) {
        return this.jbTypeDAO.findOne(id);
    }

    public TbCodeJglx findJglxPage(Integer id) {
        return this.jglxDAO.findOne(id);
    }

    public Page<TbCodeJglx> findJglxPage(Pageable pageable) {

        return this.jglxDAO.findAll(pageable);
    }

    public TbCodeJzyt findJzytPage(Integer id) {
        return this.jzytDAO.findOne(id);
    }

    public Page<TbCodeJzyt> findJzytPage(Pageable pageable) {

        return this.jzytDAO.findAll(pageable);
    }

    public List<Map<String, Object>> findJzytTreeList() {
        List<TbCodeJzyt> list = this.jzytDAO.findAll();
        List<Map<String, Object>> maps = new ArrayList<>();
        for (TbCodeJzyt jzyt : list) {
            Map<String, Object> m = new HashMap<>();
            m.put("id", jzyt.getId());
            m.put("text", jzyt.getName());
            maps.add(m);
        }
        return maps;
    }

    public TbCodeQdgf findQdgfPage(Integer id) {
        return this.qdgfDAO.findOne(id);
    }

    public Page<TbCodeQdgf> findQdgfPage(Pageable pageable) {
        return this.qdgfDAO.findAll(pageable);
    }

    public List<Map<String, Object>> findQdgfTreebox() {
        List<Map<String, Object>> maps = new ArrayList<>();
        List<TbCodeQdgf> list = this.qdgfDAO.findAll();
        for (TbCodeQdgf qdgf : list) {
            Map<String, Object> m = new HashMap<>();
            m.put("id", qdgf.getId());
            m.put("text", qdgf.getName());
            maps.add(m);
        }
        return maps;
    }

    public TbCodeReportState findReportStateById(Integer id) {
        return this.reportStateDAO.findOne(id);
    }

    public Page<TbCodeReportState> findReportStatePage(PageRequest pageRequest) {

        return this.reportStateDAO.findAll(pageRequest);
    }

    public TbScale findScaleById(UUID id) {
        return this.scaleDAO.findOne(id);
    }

    public TbCodeTaskState findTaskStateById(Integer id) {

        return this.taskStateDAO.findOne(id);
    }

    public Page<TbCodeTaskState> findTaskStatePage(PageRequest pageRequest) {
        Page<TbCodeTaskState> page = this.taskStateDAO.findAll(pageRequest);
        return page;
    }

    public TbCodeXmState findxmStateById(Integer id) {

        return this.xmStateDAO.findOne(id);
    }

    public TbCodeXmType findXmTypeById(Integer id) {
        return this.xmTypeDAO.findOne(id);
    }

    public Page<TbCodeJbType> getJbTypePage(PageRequest pageRequest) {
        Page<TbCodeJbType> page = this.jbTypeDAO.findAll(pageRequest);
        return page;
    }

    public Page<TbScale> getScalePageByProjectId(Integer xmTypeId, Pageable pageable) {
        Page<TbScale> page = this.scaleDAO.findByXmTypeId(xmTypeId, pageable);
        return page;
    }

    public Page<TbCodeXmState> getXmStatePage(PageRequest pageRequest) {
        Page<TbCodeXmState> page = this.xmStateDAO.findAll(pageRequest);
        return page;
    }

    public List<Map<String, Object>> getXmTypeComboTree() {
        List<Map<String, Object>> tree = new ArrayList<>();
        List<TbCodeXmType> list = this.xmTypeDAO.findAll();
        for (TbCodeXmType x : list) {
            Map<String, Object> node = new HashMap<>();
            node.put("id", x.getId());
            node.put("text", x.getName());
            tree.add(node);
        }
        return tree;
    }

    public Page<TbCodeXmType> getXmTypePage(PageRequest pageRequest) {
        Page<TbCodeXmType> page = this.xmTypeDAO.findAll(pageRequest);
        return page;
    }

    public void jbTypeSave(TbCodeJbType jbType) {
        this.jbTypeDAO.save(jbType);

    }

    public void reportStateSave(TbCodeReportState reportState) {
        this.reportStateDAO.save(reportState);

    }

    public void saveDegf(TbCodeDegf degf) {
        this.degfDAO.save(degf);

    }

    public void saveJglx(TbCodeJglx jglx) {
        this.jglxDAO.save(jglx);
    }

    public void saveJzyt(TbCodeJzyt jzyt) {
        this.jzytDAO.save(jzyt);

    }

    public void saveQdgf(TbCodeQdgf qdgf) {
        this.qdgfDAO.save(qdgf);

    }

    public void scaleSave(TbScale scale) {
        this.scaleDAO.save(scale);

    }

    public void taskStateSave(TbCodeTaskState taskState) {
        this.taskStateDAO.save(taskState);

    }

    public void xmStateSave(TbCodeXmState xmState) {
        this.xmStateDAO.save(xmState);

    }

    public void xmTypeSave(TbCodeXmType xmType) {
        this.xmTypeDAO.save(xmType);

    }

	public List<Map<String, Object>> xmStateCombo() {
		List<Map<String, Object>> maps=new ArrayList<>();
		List<TbCodeXmState> list = xmStateDAO.findAll();
		for (TbCodeXmState xmState : list) {
			HashMap<String, Object> node=new HashMap<>();
			node.put("id", xmState.getId());
			node.put("text", xmState.getName());
			maps.add(node);
		}
		return maps;
	}

}
